﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.BusinessModel.Rules;
using iLangl.BusinessModel.Managers;
using iLangl.Transport.Communication;

namespace iLangl.BusinessModel.Actions.Customers
{
	/// <summary>
	/// Proceed Request from Customer to register new project.
	/// 1. Save project to iLangl DB.
	/// 2. Transfer project to Translation Provider format
	/// 3. Send Project to Translation Provider 
	/// 4. Send response to Customer
	/// </summary>
	public class RegisterNewProject : BaseSecurityAction
	{
		public RegisterNewProject() : base()
		{
			ResponseContext = new ProjectResponseContext();
			RequestContext = new ActionResultRequestContext();
		}

		protected override void _execute()
		{
			Project projectToRegister = (Project)ResponseContext.GetContext();

			if (projectToRegister == null)
			{
				IAlert alert = Facade.Instance.AlertManager.CreateAlert(this,new NullReferenceException());
				alert.AddAction(this);
				Facade.Instance.AlertManager.RaiseAlert(alert);
			}

			//Check by license key if data specified properly

			Contact contact = projectToRegister.Owner;
			ICompany company = null;
			if(projectToRegister.Owner != null)
				company = projectToRegister.Owner.Company;

			PluginLicense license = PluginManager.Instance.GetLicense(PluginLiceneKey);
			projectToRegister.LicenseKey = license.ID;

			if (license == null)
			{
				IAlert alert = Facade.Instance.AlertManager.CreateAlert(this);
				alert.AddError(new Error(string.Format("Plugin License Key='{0}' is invalid!", PluginLiceneKey),"", eSeverity.High));
				Facade.Instance.AlertManager.RaiseAlert(alert);
			}

			if (projectToRegister.Owner == null)
			{
				projectToRegister.Owner = license.Contact;
			}
			else if(projectToRegister.Owner.Company != null)
			{
				//If company Id not equal generate alert
				if (projectToRegister.Owner.Company.Id != license.Contact.Company.Id)
				{
					//TODO: Generate Alert. Different Company
				}
			}
			
			
			//Language Mapping
			ProjectManager.Instance.ToLocalLanguagesMapping(projectToRegister, license);

			//Validate Final Result
			IList<IError> servererrors = new ProjectServerValidation().Validate(projectToRegister);

			if (servererrors != null)
			{
				//TODO: Handle exceptions
				ErrorStatistic statistic = ErrorManager.GetStatistic(servererrors);

				IAlert alert = Facade.Instance.AlertManager.CreateAlert(this, servererrors);
				alert.AddAction(this);
				Facade.Instance.AlertManager.RaiseAlert(alert);
			}
			
			try
			{
				ProjectManager.Instance.RegisterProject(projectToRegister);
			}
			catch (System.Exception ex)
			{
				Facade.Instance.AlertManager.RaiseAlert(ex);
			}

			ActionResult result = new ActionResult();
			RequestContext.SetContext(result);
		}

		protected override string GetName()
		{
			return "RegisterNewProject";
		}

		protected override string GetVersion()
		{
			return "1";
		}

	}
}
